package com.action.wx.bank;

import com.bean.Bank_card;
import com.common.BaseAction;
import com.common.ModelAndJson;
import com.common.SystemConfig;
import com.interceptor.CheckWxLogin;
import com.service.bank.BankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by lvjianqing on 2017/9/28.
 */
@Controller
@RequestMapping("wx/bankcard")
public class BankCardAction extends BaseAction {
    @Autowired
    private BankCardService bankCardService;

    @CheckWxLogin(name = "LoginCheck", failed = "/wx/loginError.htm")
    @RequestMapping("list")
    public ModelAndView list(ModelMap modelMap) {
        HttpSession session = this.getSession();
        Integer mid = (Integer) session.getAttribute(SystemConfig.MemberIdSession);
        List<Bank_card> list = bankCardService.getBankCardList(mid);
        modelMap.put("bclist", list);
        return new ModelAndView("/wx/bank/bank_card_list", modelMap);
    }

    @CheckWxLogin(name = "LoginCheck", failed = "/wx/loginError.htm")
    @RequestMapping("add")
    public ModelAndView addBankCard(ModelMap modelMap, int from) {
        modelMap.put("from", from);
        return new ModelAndView("/wx/bank/bank_card_add", modelMap);
    }

    @CheckWxLogin(name = "LoginCheck", failed = "/wx/loginError.htm")
    @RequestMapping("modify")
    public ModelAndView modify(ModelMap modelMap, Integer bcId) {
        HttpSession session = this.getSession();
        Integer mid = (Integer) session.getAttribute(SystemConfig.MemberIdSession);
        if (bcId != null && bcId > 0) {
            Bank_card bc = bankCardService.selectByPrimaryKey(bcId);
            if(bc.getMemberid().equals(mid)){
                modelMap.put("bc", bc);
            }
        }
        return new ModelAndView("/wx/bank/bank_card_modify", modelMap);
    }

    @CheckWxLogin(name = "LoginCheck", failed = "/wx/loginError.htm")
    @RequestMapping("save")
    public ModelAndView save(ModelMap modelMap, BankCardVo vo, int from) {
        HttpSession session = this.getSession();
        Integer mid = (Integer) session.getAttribute(SystemConfig.MemberIdSession);
        Bank_card bc = new Bank_card();
        bc.setBankaccount(vo.getBankaccount());
        bc.setBankname(vo.getBankname());
        bc.setBankplace(vo.getBankplace());
        bc.setRealname(vo.getRealname());
        bc.setMemberid(mid);
        Integer bcid = vo.getBcid();
        if(bcid==null || bcid==0) {
            bankCardService.insertSelective(bc);
        }else{
            bc.setId(vo.getBcid());
            bankCardService.updateByPrimaryKeySelective(bc);
        }
        if (from == 0) {
            return new ModelAndView("redirect:/wx/bankcard/list.htm", modelMap);
        } else {
            return new ModelAndView("redirect:/wx/cash/rebate.htm", modelMap);
        }
    }

    @CheckWxLogin(name = "LoginCheck", failed = "/wx/loginError.htm")
    @RequestMapping("del")
    @ResponseBody
    public String del(ModelAndJson model, Integer bcId) {
        HttpSession session = this.getSession();
        Integer mid = (Integer) session.getAttribute(SystemConfig.MemberIdSession);
        Bank_card bc = bankCardService.selectByPrimaryKey(bcId);
        if (bc != null && bc.getMemberid() != null && bc.getMemberid().equals(mid)) {
            if (bcId != null && bcId > 0) {
                bankCardService.deleteByPrimaryKey(bcId);
                model.setTrue("删除成功");
            }
        } else {
            model.setFalse("-1", "无法删除");
        }
        return model.toString();
    }
}
